//给你一个字符串 s，找到 s 中最长的回文子串。 
//
// 
//
// 示例 1： 
//
// 
//输入：s = "babad"
//输出："bab"
//解释："aba" 同样是符合题意的答案。
// 
//
// 示例 2： 
//
// 
//输入：s = "cbbd"
//输出："bb"
// 
//
// 示例 3： 
//
// 
//输入：s = "a"
//输出："a"
// 
//
// 示例 4： 
//
// 
//输入：s = "ac"
//输出："a"
// 
//
// 
//
// 提示： 
//
// 
// 1 <= s.length <= 1000 
// s 仅由数字和英文字母（大写和/或小写）组成 
// 
// Related Topics 字符串 动态规划 
// 👍 3075 👎 0

package com.leetcode.editor.cn;

//Java：最长回文子串
class P5LongestPalindromicSubstring {
    public static void main(String[] args) {
        Solution solution = new P5LongestPalindromicSubstring().new Solution();
        // TO TEST
        System.out.println(solution.longestPalindrome("abcdedcniw"));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String longestPalindrome(String s) {
            if (s == null || s.length() == 1) {
                return s;
            }
            //1.中心扩散
            return expandAroundCenter(s);
            //2.Manacher算法
//            return manacher(s);
        }

        private String manacher(String s) {
            //1.转换成字符数字，添加特定字符
            char[] charArray = s.toCharArray();
            char[] newCharArray = preProcess(charArray);
            int n = newCharArray.length;
            int[] results = new int[n];
            int center = 0, right = 0;
            //2.遍历新数组，记录每个位置i的最长回文半径results[i]
            for (int i = 1; i < n - 1; i++) {
                //2.1选定当前中心位置center以及右边界right,以中心位置计算i的镜像位置i_mirror
                int i_mirror = 2 * center - i;
                //2.2如果i不超过右边界，则results[i]取right-i与results[i_mirror]的较小值
                if (i < right){
                    results[i] = Math.min(right - i, results[i_mirror]);
                }
                //2.3以i为中心,results[i]为半径再扩展比对，并更新results[i]
                while (newCharArray[i + results[i] + 1] == newCharArray[i - results[i] - 1]) {
                    results[i]++;
                }
                //2.4如果i+results[i]超过右边界则更新center和right(拉车)
                if (i + results[i] > right) {
                    center = i;
                    right = i + results[i];
                }
            }
            //3.遍历results寻找最大的回文半径
            int maxLen = 0;
            int currentIndex = 0;
            for (int i = 0; i < results.length; i++) {
                if (results[i] > maxLen) {
                    maxLen = results[i];
                    currentIndex = i;
                }
            }
            //4.换算成原始字符串起始位置
            int start = (currentIndex - maxLen)/2;
            return new String(charArray, start, maxLen);
        }

        private char[] preProcess(char[] charArray) {
            char[] newCharArray = new char[charArray.length * 2 + 3];
            int index = 0;
            newCharArray[index++] = '^';
            for (char c : charArray) {
                newCharArray[index++] = '#';
                newCharArray[index++] = c;
            }
            newCharArray[index++] = '#';
            newCharArray[index++] = '$';
            return newCharArray;
        }

        private String expandAroundCenter(String s) {
            int start = 0, maxLen = 1;
            char[] charArray = s.toCharArray();
            for (int i = 0; i < s.length(); i++) {
                //以第i位为中心的奇数长度回文字串
                int l1 = expand(charArray, i, i);
                //以第i位为左中心的偶数位长度回文字串
                int l2 = expand(charArray, i, i + 1);
                int len = Math.max(l1, l2);
                if (len > maxLen) {
                    start = i - (len - 1) / 2;
                    maxLen = len;
                }
            }
            return new String(charArray, start, maxLen);
        }

        private int expand(char[] chars, int left, int right) {
            while (left >= 0 && right < chars.length && chars[left] == chars[right]) {
                left--;
                right++;
            }
            return right - left + 1 - 2;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}